<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\template\container\xhtml\instructions;

interface IAsset
{
}

class ContainerAsset extends Concrete implements IAsset, IConcrete
{
  const CONFIG_DOMAIN_ASSETS = 'assets';

  public function start()
  {
    $oTrigger = new \crcl\base\template\Trigger();
    $oTrigger->setScope(
        \crcl\base\template\container\xhtml\Instruction::SCOPE_NEXT_ATTRIBUTE_REPLACE);
    $oTrigger->setInstruction($this);

    return $oTrigger;
  }

  public function executeNext($sTag, $aAttributes)
  {
    $sUrl = $this->getAttribute('asset');
    $sOutput = $this->compileVar($sUrl);

    switch ($sTag)
    {
      case 'link':
        $sAttribute = 'href';
        break;
      case 'img':
      case 'script':
        $sAttribute = 'src';
        break;
      case 'video':
        $sAttribute = 'poster';
        break;
      default:
        //invalid tag
        throw new Exception('Invalid tag.', Exception::ERROR_INVALID_TAG, array('tag' => $sTag));
    }
    $this->aContentAttributes = array_merge($aAttributes, array($sAttribute => $sOutput));
  }

  /** PRIVATE *************************************************************************************/

  protected $aAllowedAttributes = array('asset');

  private function compileVar($sUrl)
  {
    if (\strpos($sUrl, '$') !== \FALSE)
    {
      //dynamic and mixed
      $sUrl = \str_replace('\'', '\\\'', $sUrl);
      $sUrl = '{$oValues->out(\'' . $sUrl . '\')}';
    }

    $sVersion = \crcl\base\Config::reader()->get(self::CONFIG_DOMAIN_ASSETS, 'version');
    $sRoot = \trim(\crcl\base\Config::reader()->get(self::CONFIG_DOMAIN_ASSETS, 'url'));
    $sRoot = \crcl\base\String::getInstance($sRoot)->doEndWith('/')->toString();
    $sRootFile = \crcl\base\application\Path::getDirectory(
        \crcl\base\application\Path::DIR_ASSETS_TEMPLATES);

    $sRootFileStatic = \crcl\base\application\Path::getDirectory(
        \crcl\base\application\Path::DIR_ASSETS_HTTPDOCS);

    $sTheme = '';
    if (\crcl\base\Mvc::hasTheme())
    {
      $sTheme = \crcl\base\Mvc::getTheme() . '/';
    }
    $sUrlOut = $sRoot . $sTheme . $sUrl . '?version=' . $sVersion;
    $sFileOutStatic = $sRootFileStatic . $sTheme . $sUrl;
    //var_dump($sFileOutStatic);
    $sFileOut = $sRootFile . $sTheme . $sUrl;
    //var_dump($sFileOut);
    if (!\file_exists($sFileOut) and !\file_exists($sFileOutStatic))
    {
      $sUrlOut = $sRoot . 'default/' . $sUrl . '?version=' . $sVersion;
    }
    return $sUrlOut;
  }
}